package com.gcalsync;

import java.util.TimeZone;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import com.gcalsync.component.*;
import com.gcalsync.log.*;

/**
 * Copyright Thomas Oldervoll
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * <p/>
 * <p/>
 * TODO:
 * - Allow batch sync (daily), configurable
 * - Future: add other calendars by adding the private URLs and a tag to be used as prefix. Ex: Ann# Norskkurs.
 * Methods to add: 1. Type in. 2. Send SMS. 3. Add event i Gcal with titel GcalSync add Ann http://www.goog....
g * - TODO: Grab all calendars for a user, if necessary by screenscraping
 *
 * @author Thomas Oldervoll, thomas@zenior.no
 */
public class GCalSync extends MIDlet {

    private Display display;

    protected void startApp() throws MIDletStateChangeException {
        display = Display.getDisplay(this);
        if (checkRequirements()) {
            setDependencies();
            Components.login.handle();
        }
    }

    private boolean checkRequirements() {
        String pimVersion = System.getProperty("microedition.pim.version");
        if (pimVersion == null) {
            Form messageForm = new Form("Not supported");
            messageForm.append("Sorry, your phone is not supported");
            Command exitCommand = new Command("Exit", Command.EXIT, 2);
            messageForm.addCommand(exitCommand);
            messageForm.setCommandListener(new CommandListener() {
                public void commandAction(Command command, Displayable displayable) {
                    notifyDestroyed();
                }
            });
            display.setCurrent(messageForm);
            return false;
        }
        return true;
    }

    private void setDependencies() {
        ErrorHandler.log = new StringBuffer();
        ErrorHandler.display = display;
        MVCComponent.display = display;
		Components.login.setMidlet(this);
    }

    protected void pauseApp() {
    }

    protected void destroyApp(boolean b) {
    }

}

